package com.vn.ducktohome.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.actions.RepeatAction;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.vn.ducktohome.enums.ScreenEnum;
import com.vn.ducktohome.gamelogic.GameControl;
import com.vn.ducktohome.helper.SoundMusic;
import com.vn.ducktohome.screen.play.LevelHelper;

public class MenuScreen extends BaseScreen {

	private int width;
	private TextButton tbNewGame, tbAbout, tbQuitGame;
	private Image imgCaSauBig, imgLogo;
	private SoundMusic soundMusic;

	@Override
	public void show() {
		super.show();
		Image backGround = new Image(GameControl.getAtlas().findRegion("menu/background"));
		stage.addActor(backGround);
		imgCaSauBig = new Image(GameControl.getAtlas().findRegion("menu/ca-sau-big"));
		imgCaSauBig.setX(60);
		imgCaSauBig.setY(480 - imgCaSauBig.getHeight());
		stage.addActor(imgCaSauBig);
		
		imgLogo = new Image(GameControl.getAtlas().findRegion("play/title"));
		imgLogo.setX(280);
		imgLogo.setY(350);
		stage.addActor(imgLogo);
		
		soundMusic = new SoundMusic();
		soundMusic.setX(630);
		soundMusic.setY(400);
		stage.addActor(soundMusic);
		initVitBig();
		initButton();
		initClick();
	}

	private void initVitBig() {
		Image imgVitBig1 = new Image(GameControl.getAtlas().findRegion("menu/vit-big"));
		Image imgVitBig2 = new Image(GameControl.getAtlas().findRegion("menu/vit-big"));
		imgVitBig2.setX(100);
		imgVitBig2.setY(20);
		Image imgVitBig3 = new Image(GameControl.getAtlas().findRegion("menu/vit-big"));
		imgVitBig3.setX(200);
		imgVitBig3.setY(10);
		Group gVitBig = new Group();
		gVitBig.addActor(imgVitBig1);
		gVitBig.addActor(imgVitBig2);
		gVitBig.addActor(imgVitBig3);
		gVitBig.setX(-300);
		gVitBig.setY(40);
		stage.addActor(gVitBig);
		MoveToAction moveTo = new MoveToAction();
		moveTo.setX(900);
		moveTo.setY(40);
		moveTo.setDuration(7f);
		MoveToAction moveTo2 = new MoveToAction();
		moveTo2.setX(-400);
		moveTo2.setY(40);
		moveTo2.setDuration(0);
		SequenceAction sequen = new SequenceAction();
		sequen.addAction(moveTo);
		sequen.addAction(moveTo2);
		RepeatAction repeat = new RepeatAction();
		repeat.setAction(sequen);
		repeat.setCount(RepeatAction.FOREVER);
		gVitBig.addAction(repeat);
	}

	private void initButton() {
		width = 150;
		tbNewGame = new TextButton("New Game", GameControl.getMySkin(), "button-red");
		tbNewGame.setX((stage.getWidth() - width) / 2);
		tbNewGame.setY(300);
		tbNewGame.setWidth(width);
		tbNewGame.setHeight(50);
		stage.addActor(tbNewGame);
		
		tbAbout = new TextButton("About", GameControl.getMySkin(), "button-red");
		tbAbout.setX(tbNewGame.getX());
		tbAbout.setY(230);
		tbAbout.setWidth(width);
		tbAbout.setHeight(tbNewGame.getHeight());
		stage.addActor(tbAbout);
		
		tbQuitGame = new TextButton("Quit Game", GameControl.getMySkin(), "button-red");
		tbQuitGame.setX(tbNewGame.getX());
		tbQuitGame.setY(160);
		tbQuitGame.setWidth(width);
		tbQuitGame.setHeight(tbNewGame.getHeight());
		stage.addActor(tbQuitGame);
		
	}
	
	private void initClick(){
		tbNewGame.addListener(new ClickListener(){

			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				GameControl.getScreenManager().createScreen(ScreenEnum.SELECT_LEVEL_SCREEN);
				GameControl.getScreenManager().getScreen().show();
			}
		});
		
		tbAbout.addListener(new ClickListener(){

			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
			}
		});
		
		tbQuitGame.addListener(new ClickListener(){

			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				LevelHelper.writeLevel();
				Gdx.app.exit();
			}
		});
	}
}